package model;

import semaphore.Semaphore;


public class DiningPermissionToken {
	private final Semaphore mutex;
	private boolean tokenAvailable = true;
	
	public DiningPermissionToken() {
		this.mutex = new Semaphore(1);
	}

	/**
	 * Obtains the token when it is avaiable.
	 * Waits until the token is available when the token is not available.
	 */
	public void get(){
		this.mutex.down();
		this.tokenAvailable = false;
	}
	
	/**
	 * Gives the token back.
	 * @throws Error when called when a token is still there.
	 */
	public void put(){
		if(this.tokenAvailable){
			throw new Error();
		} else {
			this.tokenAvailable = true;
			this.mutex.up();
		}
	}
}